<template>
  <div
    class="page-container page-container-havePagination"
    :class="{'page-container-haveFooter':$store.state.settings.showFooter}"
  >
    <!-- 工具栏 -->
    <div class="head-search-container">
      <!-- 搜索 -->
      <el-form
        :inline="true"
        label-position="right"
        label-width="74px"
        size="small"
        :model="searchQuery"
        class="demo-form-inline"
      >
        <!-- <el-form-item label="机构名称">
          <el-select v-model="searchQuery.orgCode" filterable placeholder="机构名称" class="search-btn">
            <el-option
              v-for="item in orgs"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item label="选择月份">
          <el-date-picker
            class="search-btn"
            value-format="yyyy-MM-dd 00:00:00"
            v-model="time"
            @change="changeDate"
            type="month"
            :clearable="false"
            placeholder="选择月"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="success" class="greenColor" @click="search">查询</el-button>
          <el-button size="small" @click="reloadSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格渲染 -->
    <el-table
      header-row-class-name="table-header-style"
      :row-class-name="tableRowStyle"
      :data="tableData"
      tooltip-effect="dark"
      show-summary
      style="width: 100%"
      v-loading="loading"
    >
      <el-table-column type="index" label="序号" fixed="left"></el-table-column>
      <el-table-column prop="orgName" label="机构名称" fixed="left" show-overflow-tooltip></el-table-column>
      <el-table-column prop="day1" label="1日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day2" label="2日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day3" label="3日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day4" label="4日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day5" label="5日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day6" label="6日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day7" label="7日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day8" label="8日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day9" label="9日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day10" label="10日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day11" label="11日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day12" label="12日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day13" label="13日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day14" label="14日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day15" label="15日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day16" label="16日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day17" label="17日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day18" label="18日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day19" label="19日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day20" label="20日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day21" label="21日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day22" label="22日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day23" label="23日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day24" label="24日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day25" label="25日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day26" label="26日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day27" label="27日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day28" label="28日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day29" label="29日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day30" label="30日" :width="cellWidth"></el-table-column>
      <el-table-column prop="day31" label="31日" :width="cellWidth"></el-table-column>
      <el-table-column prop="totalNum" label="总数" fixed="right"></el-table-column>
    </el-table>
    <!-- 分页器 -->
    <!-- <TablePagination
      :total="total"
      :currentPage="currentPage"
      :pageSize="pageSize"
      @changeSize="changeSize"
      @changePage="changePage"
      :class="['table-pagination',{'table-pagination-haveFooter':$store.state.settings.showFooter}]"
    />-->
  </div>
</template>
<script>
import initData from "@/mixins/req";
import initORG from "@/mixins/org";
import store from "@/store";
import { setSearchValue, arryGroup, deepClone,compare } from "@/utils/index";
export default {
  name: "dailyRegisters",
  mixins: [initORG, initData],
  data() {
    return {
      searchQuery: {
        startTime: "", // 年月
        endTime: "" // 年月
      },
      time: "",
      cellWidth: "38px"
    };
  },
  created() {
    // this.initORG();
    this.setDefaultValue();
    this.search();
  },
  methods: {
    dealData(data) {
      let newArr = arryGroup(deepClone(data), "orgName");
      newArr.forEach(item => {
        let totalNum = 0;
        for (let i = 1; i <= 31; i++) {
          let time = item.data.filter(item2 => item2.time == i);
          item["day" + i] = time.length > 0 ? time[0].num : 0;
          totalNum += item["day" + i];
        }
        item.totalNum = totalNum;
      });
      this.tableData = newArr.sort(compare('totalNum'));
    },
    search() {
      this.init().then(res => {
        this.dealData(res.data);
      });
    },
    changeDate(val) {
      this.searchQuery.startTime = val;
      this.searchQuery.endTime =
        val.split("-")[0] + "-" + val.split("-")[1] + "-31 23:59:59";
    },
    setDefaultValue() {
      let date = new Date();
      this.time=date;
      this.searchQuery.startTime =
        date.getFullYear() +
        "-" +
        (parseInt(date.getMonth()) + 1) +
        "-" +
        "01 00:00:00";
      this.searchQuery.endTime =
        date.getFullYear() +
        "-" +
        (parseInt(date.getMonth()) + 1) +
        "-31 23:59:59";
    },
    reloadSearch() {
      setSearchValue(this, this.searchQuery);
      this.setDefaultValue();
      this.search();
    },
    beforeInit() {
      this.url = store.getters["api/dailyRegisterUser"];
      this.searchParams = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        ...this.searchQuery
      };
      return true;
    },
    look(row) {
      this.$refs.detailsPopup.show = true;
      this.$refs.detailsPopup.form = row;
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "~@/assets/styles/page.scss";
@import "~@/assets/styles/table.scss";
/deep/ .table-header-style th {
  font-size: 8px;
  .cell {
    padding: 0;
  }
}
/deep/ .table-row-style td,
/deep/ .el-table__footer td {
  font-size: 10px;
  .cell {
    padding: 0;
    text-align: center;
  }
}
.search-btn {
  width: 180px !important;
}
</style>
